package com.ruimei.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruimei.entitys.*;
import com.ruimei.mapper.AttendanceMapper;
import com.ruimei.service.AttendanceService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.ruimei.util.postUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class AttendanceServiceImpl
  implements AttendanceService
{
  @Resource
  AttendanceMapper attendanceMapper;
  @Resource
  RedisTemplate<String,String> redisTemplate;

  @Override
  @CacheEvict(value={"myCache"}, allEntries=true)
  public Map<String, Object> insertAttendance(Attendance attendance, Teacher teacher)
  {
    Map<String, Object> map = new HashMap();
    attendance.setTeacherId(teacher.getId());
    attendance.setTeacherName(teacher.getFullName());
    attendance.setAttendanceId(System.currentTimeMillis() + "");
    attendance.setCreateTime(new Date());
    Integer insertAttendance = this.attendanceMapper.insertAttendance(attendance);

    if (insertAttendance.intValue() == 1)
    {
      map.put("status", Boolean.valueOf(true));
      map.put("message", "添加成功");
    }
    else
    {
      map.put("status", Boolean.valueOf(false));
      map.put("message", "添加失败");
    }
    return map;
  }

  @Override
  @Cacheable(value={"myCache"}, key="'getClassByTeacherId'+ #teacherId")
  public Map<String, Object> getClassByTeacherId(String teacherId)
  {


    List<RmClass> list;

    if(postUtil.post(redisTemplate,teacherId,attendanceMapper)){
      list = this.attendanceMapper.getClassRoom();  //查询所有班级
    }else{
      list = this.attendanceMapper.getClassByTeacherId(teacherId);
    }

    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    return map;
  }

  @Override
  public Map<String, Object> getClassRoom() {
    List<RmClass> list = this.attendanceMapper.getClassRoom();

    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    return map;
  }

  @Cacheable(value={"myCache"}, key="'getStudentsByClassId'+ #classId")
  public Map<String, Object> getStudentsByClassId(String classId)
  {
    List<Student> list = this.attendanceMapper.getStudentsByClassId(classId);
    
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
  
  @Cacheable(value={"myCache"}, key="'getAttendanceByStudentName'+ #classId + #studentName + #beginTime + #endTime")
  public Map<String, Object> getAttendanceByStudentName(String classId, String studentName, String beginTime, String endTime)
  {
    List<AttendanceStat> list = this.attendanceMapper.getAttendanceByStudentName(classId, studentName, beginTime, endTime);
    Integer LATE = Integer.valueOf(0);
    Integer LEAVE_EARLY = Integer.valueOf(0);
    Integer ABSENTEEISM = Integer.valueOf(0);
    Integer COMPASSIONATE_LEAVE = Integer.valueOf(0);
    Integer SICK_LEAVE = Integer.valueOf(0);
    Integer OTHERS = Integer.valueOf(0);
    for (AttendanceStat a : list)
    {
      LATE = Integer.valueOf(LATE.intValue() + Integer.valueOf(a.getLATE()).intValue());
      LEAVE_EARLY = Integer.valueOf(LEAVE_EARLY.intValue() + Integer.valueOf(a.getLEAVE_EARLY()).intValue());
      ABSENTEEISM = Integer.valueOf(ABSENTEEISM.intValue() + Integer.valueOf(a.getABSENTEEISM()).intValue());
      COMPASSIONATE_LEAVE = Integer.valueOf(COMPASSIONATE_LEAVE.intValue() + Integer.valueOf(a.getCOMPASSIONATE_LEAVE()).intValue());
      SICK_LEAVE = Integer.valueOf(SICK_LEAVE.intValue() + Integer.valueOf(a.getSICK_LEAVE()).intValue());
      OTHERS = Integer.valueOf(OTHERS.intValue() + Integer.valueOf(a.getOTHERS()).intValue());
    }
    Object all = new HashMap();
    ((Map)all).put("LATE", LATE);
    ((Map)all).put("LEAVE_EARLY", LEAVE_EARLY);
    ((Map)all).put("ABSENTEEISM", ABSENTEEISM);
    ((Map)all).put("COMPASSIONATE_LEAVE", COMPASSIONATE_LEAVE);
    ((Map)all).put("SICK_LEAVE", SICK_LEAVE);
    ((Map)all).put("OTHERS", OTHERS);
    ((Map)all).put("list", list);
    
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", all);
    
    return map;
  }
  
  public List<Attendance> parentAttendance(String id, String type, String attendanceId,String beginTime, String endTime)
  {
    if("all".equals(type)){
      type = null;
    }
    return this.attendanceMapper.selectAttendance(id, type,attendanceId, beginTime, endTime);
  }
  
  @Cacheable(value={"myCache"}, key="'getAttendanceByTeacherId'+ #teacherId + #attendanceId + #attendanceType + #studentName + #classId + #beginTime + #endTime")
  public Map<String, Object> getAttendanceByTeacherId(String teacherId, String attendanceId, String attendanceType, String studentName, String classId, String beginTime, String endTime)
  {
    List<Attendance> list = this.attendanceMapper.getAttendanceByTeacherId(teacherId, attendanceId, attendanceType, studentName, classId, beginTime, endTime);
    
    Map<String, Object> map = new HashMap();
    map.put("status", Boolean.valueOf(true));
    map.put("message", "查询成功");
    map.put("count", Integer.valueOf(list.size()));
    map.put("data", list);
    
    return map;
  }
}


/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\AttendanceServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */